﻿using System;
using System.Windows.Forms;
using Zipun.VkOnlineMusic.Api;
using Zipun.VkOnlineMusic.Api.Domain;
using Zipun.VkOnlineMusic.View.WinForms.Properties;

namespace Zipun.VkOnlineMusic.View.WinForms
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
            VkToken = null;
        }

        public VkToken VkToken { get; private set; }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            loginWebBrowser.Url = new Uri(VkToken.LoginUrlString);
        }

        private void loginWebBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            if (!e.Url.AbsoluteUri.StartsWith(VkToken.RedirectUriString)) 
                return;

            try
            {
                VkToken = new VkToken(e.Url.AbsoluteUri);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Resources.LoginForm_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = DialogResult.Cancel;
                return;
            }
            DialogResult = DialogResult.OK;
            Close();
        }
    }
}
